package com.bestom.t9.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bestom.t9.R;

import com.bestom.t9mod.base.service.SerialPortService;
import com.bestom.t9mod.core.BaseActivity;
import com.bestom.t9mod.core.api.PowerApi;
import com.bestom.t9mod.core.api.T9IntentApi;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private Context mContext;
    private Activity mActivity;
    private Unbinder mUnbinder;

    @BindView(R.id.liul_bt) Button liul_bt;
    @BindView(R.id.wifi_bt) Button wifi_bt;
    @BindView(R.id.bt_bt) Button bt_bt;
    @BindView(R.id.shutdown_bt) Button shutdown_bt;
    @BindView(R.id.reboot_bt) Button reboot_bt;
    @BindView(R.id.idcard_bt) Button idcard_bt;
    @BindView(R.id.kpa_bt) Button kpa_bt;
    @BindView(R.id.tts_bt) Button tts_bt;
    @BindView(R.id.power_bt) Button power_bt;
    @BindView(R.id.power_state) TextView power_state;
    @BindView(R.id.power_electric) TextView power_electric;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext=this;
        mActivity=this;
        setNavigationBarVisible(mActivity,true);
        mUnbinder=ButterKnife.bind(mActivity);

        liul_bt.setOnClickListener(this);
        wifi_bt.setOnClickListener(this);
        bt_bt.setOnClickListener(this);
        shutdown_bt.setOnClickListener(this);
        reboot_bt.setOnClickListener(this);
        idcard_bt.setOnClickListener(this);
        kpa_bt.setOnClickListener(this);
        tts_bt.setOnClickListener(this);
        power_bt.setOnClickListener(this);

//        new PowerApi().initService(getApplicationContext());
        // 设置广播信息过滤
        IntentFilter filter = new IntentFilter();
        filter.addAction("power");

        registerReceiver(powerReceiver, filter);
    }

    public static void setNavigationBarVisible(Activity activity, boolean isHide) {
        if (isHide) {
            View decorView = activity.getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        } else {
            View decorView = activity.getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(powerReceiver);
        mUnbinder.unbind();
    }

//    private SerialPortService serialPortService = new SerialPortService();
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.liul_bt:
//                startActivity(T9IntentApi.getSettings_DataUsage());

                startActivity(T9IntentApi.getSettings_CallNetWork());
                break;
            case R.id.wifi_bt:
                startActivity(T9IntentApi.getSettings_WIFI());
                break;
            case R.id.bt_bt:
                startActivity(T9IntentApi.getSettings_BLUETOOTH());
                break;
            case R.id.shutdown_bt:
//                serialPortService.writeHexToBoard("553a040093");
                startActivity(T9IntentApi.getSys_ShutDownIntent(false));
                break;
            case R.id.reboot_bt:
                startActivity(T9IntentApi.getSys_RebootIntent());
                break;
            case R.id.idcard_bt:
                startActivity(new Intent(this,IDReaderActivity.class));
                break;
            case R.id.kpa_bt:
                startActivity(new Intent(this,KPAActivity.class));
                break;
            case R.id.tts_bt:
                startActivity(new Intent(this,TTSActivity.class));
                break;
            case R.id.power_bt:
                startActivity(new Intent(this,PowerActivity.class));
                break;
            default:
                break;
        }
    }


    private BroadcastReceiver powerReceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("power".equals(action)){
                String powerElectric = intent.getStringExtra("powerElectric") + "%";
                String powerState = intent.getStringExtra("powerState");
                power_electric.setText(powerElectric);
                power_state.setText(powerState);
            }
        }
    };
}
